package freespiritsocial.io.user.viewmodel

import androidx.lifecycle.MutableLiveData
import freespiritsocial.io.user.data.location.PlaceBean
import freespiritsocial.io.user.data.model.bean.ApiRefreshResult
import freespiritsocial.io.user.data.model.bean.CommentPojo
import freespiritsocial.io.user.data.model.bean.PostPojo
import freespiritsocial.io.user.network.apiService
import freespiritsocial.io.user.network.upload.IUploadApi
import freespiritsocial.io.user.network.upload.UploadDelegate
import freespiritsocial.io.user.viewmodel.delegates.*
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.ext.requestNoCheck

class PostDetailViewModel: UserInfoViewModel(),IPostApi by PostDelegate(),IPeopleApi by PeopleDelegate(),IUploadApi by UploadDelegate() {
    val isEdit = BooleanObservableField(false)
    val data = MutableLiveData<PostPojo>(PostPojo())

    var hasChange = false

    val canUpload = BooleanObservableField(false)

    val commentResult = MutableLiveData<ApiRefreshResult<CommentPojo>>()
    val commentContent = StringObservableField()

    fun getComments(lastCommentId: Long? = null,action: () -> Unit = {}) {
        val invitationId = data.value?.invitationId
        request({
            apiService.getComments(
                hashMapOf(
                    "invitationId" to invitationId,
                    "lastCommentId" to lastCommentId,
                )
            )
        },{
            commentResult.value = ApiRefreshResult(lastCommentId == null,it,it.isNotEmpty())
            action()
        })
    }

    fun like() {
        likePost(data.value!!.invitationId)
    }

    fun likeComment(id: Long) {
        request({
            apiService.likeComment(id)
        },{

        })
    }

    fun getPost(id: Long?) {
        request({
            apiService.getInvitationDetail(id)
        },{
            data.value = it
        },isShowDialog = true)
    }

    fun comment(commentId: Long? = null,action: () -> Unit) {
        if (commentContent.get().trim().isEmpty())
            return
        request({
            apiService.comment(
                hashMapOf(
                    "commentId" to commentId,
                    "content" to commentContent.get(),
                    "invitationId" to data.value?.invitationId
                )
            )
        },{
            action()
        })
    }


    fun editPost(action: (PostPojo) -> Unit) {
        val postPojo = data.value
        val files = postPojo?.uploadFiles ?: return
        requestNoCheck({
            val list = if (files.size == 1) listOf(uploadFile(files.first()))
            else multipartFiles(files)
            postPojo.fileUrl = list?.joinToString(",") {
                it?.url ?: ""
            }
            files.filter { it.isFile.not() && it.toString().isNotEmpty() }.let { list ->
                postPojo.fileUrl += "," + list.joinToString(",") { it.toString() }
            }
            postPojo.coverUrl = postPojo.fileList.first()
            postPojo.fileUrl = postPojo.fileUrl?.removeSuffix(",")
            apiService.updateInvitation(postPojo)
            apiService.getInvitationDetail(postPojo.invitationId)
        },{
            if (it.isSucces())
                action(it.data)
            else
                toast.postValue(it.message)
        },isShowDialog = true)
    }

    fun getLocationByLatLng(lat: Double,lng: Double,callback: (PlaceBean) -> Unit) {
        requestNoCheck({
            apiService.getLocationByLatLng(latlng = "$lat,$lng")
        },{
            callback.invoke(it.results.firstOrNull() ?: return@requestNoCheck)
        })
    }
}